package com.youbenzi.dockerpool;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * 配置类
 * @author yangyingqiang
 * @time 2017年6月9日 下午6:28:07
 */
public class Config {

	/**
	 * 主机序号
	 */
	protected static int index;
	/**
	 * 连接池的主机地址列表，<index, <host/prot, value>>
	 */
	protected static Map<Integer, Map<String, String>> servers = new HashMap<>();
	/**
	 * 连接池的主机host
	 */
	protected static List<String> hosts = new ArrayList<>();
	/**
	 * docker 可使用的端口范围
	 */
	protected static int[] dockerPortRange;
	/**
	 * redis host
	 */
	protected static String redisHost;
	/**
	 * redis port
	 */
	protected static int redisPort;
	/**
	 * 最少的docker容器数量／初始化的docker容器数量
	 */
	protected static int minDockerNum = 10;
	/**
	 * 最多的docker容器数量
	 */
	protected static int maxDockerNum = 20;
	
	/**
	 * 运行模式：server/client/all
	 */
	protected static String mode;
	
	/**
	 * 创建docker 容器超时时间
	 */
	protected static final long EXPIRED_TIME = 5L * 60 * 1000;
	
	
	/**
	 * 初始化配置。
	 */
	public static void init() {
		File configFile = new File(Config.class.getResource("/docker-pool.properties").getFile());
		configure(configFile);
		
	}
	
	public static String server(String host) {
		for (Map<String, String> map : servers.values()) {
			if(map.get("host").equals(host)) {
				return map.get("host") + ":" + map.get("port");
			}
		}
		
		return null;
	}
	
	private static void configure(File configFile) {
		Properties properties = new Properties();
		try {
			properties.load(new FileReader(configFile));
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(properties);
		setDockerPortRanges(properties);
		setServers(properties);
		

		index = Integer.parseInt(properties.getProperty("index"));
		redisHost = properties.getProperty("redisHost");
		redisPort = Integer.parseInt(properties.getProperty("redisPort"));
		mode = properties.getProperty("mode");
		minDockerNum = Integer.parseInt(properties.getProperty("minDockerNum"));
		maxDockerNum = Integer.parseInt(properties.getProperty("maxDockerNum"));
	}
	
	private static void setDockerPortRanges(Properties properties) {
		String dockerPortRangeStr = properties.getProperty("dockerPortRanges");
		String[] ranges = dockerPortRangeStr.split("-");
		dockerPortRange = new int[2];
		dockerPortRange[0] = Integer.parseInt(ranges[0]);
		dockerPortRange[1] = Integer.parseInt(ranges[1]);
	}
	
	private static void setServers(Properties properties) {
		Enumeration<?> keys = properties.propertyNames();
		while (keys.hasMoreElements()) {
			String key = (String) keys.nextElement();
			if(key.startsWith("server")) {
				try {
					int index = Integer.parseInt(key.substring(6, key.length()));
					String value = properties.getProperty(key);
					String[] tmps = value.split(":");

					Map<String, String> server = new HashMap<>();
					if(tmps.length == 1 || tmps.length == 2) {
						server.put("host", tmps[0]);
						hosts.add(tmps[0]);
						if(tmps.length == 2) {
							server.put("port", tmps[1]);
						} else {
							server.put("port", "80");
						}
						servers.put(index, server);
					}
				} catch (NumberFormatException e) {
				}
			}
		}
	}
	
}
